<template>
  <div class="login-bg">
    <div class="login-center">
      <div class="login-title">LWD800激光电弧增减材控制系统</div>
      <a-card class="login-card">
        <a-form @submit.prevent="handleLogin" layout="vertical">
          <a-form-item>
            <a-input
              v-model:value="username"
              placeholder="请输入账号"
              size="large"
              :style="inputStyle"
              autocomplete="off"
            />
          </a-form-item>
          <a-form-item :validate-status="passwordError ? 'error' : ''" :help="passwordError">
            <a-input-password
              v-model:value="password"
              placeholder="请输入密码"
              size="large"
              :style="inputStyle"
              autocomplete="new-password"
            />
          </a-form-item>
          <a-form-item>
            <a-button type="primary" html-type="submit" block size="large" :style="buttonStyle"
              >登 录</a-button
            >
          </a-form-item>
        </a-form>
      </a-card>
      <div class="login-footer">© 2025 四川五八增材科技有限公司 版权所有</div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { useUserStore } from '../stores/user'
import { message } from 'ant-design-vue'

const username = ref('')
const password = ref('')
const passwordError = ref('')
const router = useRouter()
const userStore = useUserStore()

const inputStyle = {
  height: '4rem',
  fontSize: '2rem',
  borderRadius: '1.2rem',
  padding: '0 1.5rem',
}
const buttonStyle = {
  height: '4.5rem',
  fontSize: '2.2rem',
  borderRadius: '1.2rem',
  letterSpacing: '0.2em',
}

function handleLogin() {
  passwordError.value = ''
  if (!username.value) {
    passwordError.value = '请输入账号'
    return
  }
  if (!password.value) {
    passwordError.value = '请输入密码'
    return
  }
  if (username.value === 'admin' && password.value === 'admin') {
    userStore.login(username.value)
    message.success('登录成功！')
    router.push('/')
  } else {
    message.error('用户名或密码错误！')
  }
}

onMounted(() => {
  userStore.logout()
})
</script>

<style scoped>
.login-bg {
  width: 100vw;
  height: 100vh;
  background: #1a2233;
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 100vh;
}
.login-center {
  width: 700px;
  max-width: 90vw;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  height: 100vh;
  gap: 2.5rem;
}
.login-title {
  font-size: 2.8rem;
  font-weight: bold;
  color: #fff;
  letter-spacing: 2px;
  text-align: center;
  margin-bottom: 0.5em;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.login-card {
  width: 100%;
  background: #fff;
  border-radius: 1.5rem;
  box-shadow: 0 8px 32px 0 rgba(31, 38, 135, 0.18);
  padding: 3.5rem 2.5rem 2.5rem 2.5rem;
  margin-bottom: 0;
}
:deep(.ant-form-item) {
  margin-bottom: 2.5rem;
}
:deep(.ant-form-item:last-child) {
  margin-bottom: 0;
}
.login-footer {
  width: 100%;
  text-align: center;
  color: #bbb;
  font-size: 1.5rem;
  letter-spacing: 1px;
  margin-top: 0;
}
@media (max-width: 900px) {
  .login-center {
    width: 98vw;
    padding: 0 1vw;
    height: 100vh;
  }
  .login-card {
    padding: 2rem 0.5rem 1.5rem 0.5rem;
  }
  .login-title {
    font-size: 2rem;
  }
}
</style>
